<?php 
set_time_limit(0);
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
include '../c/config.php';
$countryArr=array('zg'=>'中国','tg'=>'泰国','xjp'=>'新加坡',
    'tw'=>'台湾','rb'=>'日本','xg'=>'香港');
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}

require_once './Classes/PHPExcel.php';

$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '昵称')
->setCellValue('B1', '晒坐标国家')
->setCellValue('C1', 'unionid')
->setCellValue('D1', 'utm_source')
->setCellValue('E1', '是会员')
->setCellValue('F1', '抽奖码')
->setCellValue('G1', '中奖情况')
->setCellValue('H1', '晒坐标时间')
->setCellValue('I1', '中奖码提交时间')
;

$query=$mysqli->query("SELECT * FROM dui where uid !='' ");
$index=2;
while ($list=$query->fetch_array()){
    $queryUser=$mysqli->query("SELECT * FROM users where openid='".$list['openid']."' limit 0,1 ");
    $userInfo=$queryUser->fetch_assoc();
    $utm_source=$userInfo['utm_source'];
    if (empty($utm_source)){
        $utm_source='original';
    }
    $queryDh=$mysqli->query("SELECT createtime FROM duihuan where uid='".$list['uid']."' limit 0,1 ");
    $zjqk='未抽奖';
    $zjTime='';
    if($queryDh->num_rows>0){
        $DhInfo=$queryDh->fetch_assoc();
        $zjTime=$DhInfo['createtime'];
        $zjqk='已中奖';
    }
    if(empty($userInfo['member'])){ 
        $member='还未检测';
    }else if ('notfound' == $userInfo['member']){ 
        $member='不是会员';
    }else{
        $member=$userInfo['member'];
    }
    $objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A'.$index, base64_decode($userInfo['name']))
    ->setCellValue('B'.$index, @$countryArr[$list['country']])
    ->setCellValue('C'.$index, $list['uid'])
    ->setCellValue('D'.$index, $utm_source)
    ->setCellValue('E'.$index, $member)
    ->setCellValue('F'.$index, $list['ma'])
    ->setCellValue('G'.$index, $zjqk)
    ->setCellValue('H'.$index, $list['createtime'])
    ->setCellValue('I'.$index, $zjTime)
    ;
    $index++;
}
$mysqli->close();
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('晒坐标用户');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="晒坐标用户.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
//$objWriter->save();
exit;